package Filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.User;

/**
 * Servlet Filter implementation class logFilter
 */
public class logFilter implements Filter {

    /**
     * Default constructor. 
     */
    public logFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
		// pass the request along the filter chain
		HttpServletRequest req=(HttpServletRequest) arg0;
		HttpSession session = req.getSession();
		HttpServletResponse resp=(HttpServletResponse) arg1;
		//ArrayList user_id=(ArrayList)session.getAttribute("user_id");
		
		if(session.getAttribute("user")!=null){
			User u = (User)session.getAttribute("user");
			System.out.println(u.getUser_id());
			arg2.doFilter(arg0, arg1);
		}
		else{
			//String msg="Haven't login! Jumping to the login page...";
			//PrintWriter out=resp.getWriter();
			//req.setAttribute("msg", msg);
			resp.sendRedirect("Login.jsp");
		}
		//chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
